package com.huangyong.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.huangyong.entity.User;
import com.huangyong.service.ArticleService;
import com.huangyong.service.CommentService;
import com.huangyong.service.UserService;
import com.huangyong.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/admin")
public class UserController {

	@Autowired
	private UserService userService;
	
//	---------------user----------------------------------------
	@RequestMapping("/login")
	public String login() {
		return "/admin/login";
	}
	
	@RequestMapping(value="main",method= {RequestMethod.POST,RequestMethod.GET})
	public String mainPage(String username, String password, HttpSession session) {
		int result = userService.login(username, password);
		if(result==UserServiceImpl.SUCCESS) {
			session.setAttribute("user", userService.findByUsername(username));
			session.setMaxInactiveInterval(60 * 60);
			return "/admin/main";			
		}else if(result == UserServiceImpl.PWDERROR) {
			System.out.println("密码错误");
			return "/admin/login";
		}else if(result == UserServiceImpl.NONEXIST) {
			System.out.println("用户名不存在");
			return "/admin/login";
		}else {
			return "";			
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "/admin/login";
	}
	
	@RequestMapping(value = "/register",produces = "text/html;charset=UTF-8")
	public String register(String username, String password, String phone, String email,HttpSession session) {
		User user = new User(username, password, phone, email);
		int result = userService.register(user);
		if (result == UserServiceImpl.SUCCESS) {
			session.setAttribute("msg", "添加成功");
			return "/admin/user/addUser";
		}else {
			session.setAttribute("msg", "添加失败");
			return "/admin/user/addUser";
		}
	}
	
	@RequestMapping(value = "/toUpdateUser/{username}",produces = "text/html;charset=UTF-8")
	public String ToUpdateUser(@PathVariable("username") String username,HttpSession session) {
		session.setAttribute("username", username);
		return "/admin/user/updateUser";
	}
	
	@RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
	public String updateUser(HttpSession session,String password,String phone, String email) {
		User user = (User) session.getAttribute("user");
		if(password!=null && password!="" && phone!=null && phone!="" && email!=null && email!="") {
			user.setPassword(userService.EncoderByMd5(password));
			user.setPhone(phone);
			user.setEmail(email);
			session.setAttribute("user", user);
			userService.update(user);
			session.setAttribute("updatemsg", "更新成功！");
			return "/admin/user/allUser";
		}			
		session.setAttribute("updatemsg", "更新失败！");
		return "/admin/user/allUser";

	}
	
	@RequestMapping(value="/allUser")
	public String AllUser(HttpSession session) {
		List<User> users = userService.selectAllUser();
		session.setAttribute("users", users);
		return "/admin/user/allUser";
	}
	@RequestMapping(value="/deleteUser/{username}")
	public String deleteUser(@PathVariable("username") String username,HttpSession session) {
		userService.deleteUser(username);
		return AllUser(session);
	}
	

}
